package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.swing.JFrame;

import view.*;

import event.numberevent.ListenedNumber;
import event.numberevent.NumberEvent;

public class SignalSupporterMainFrame extends JFrame implements ActionListener,NumberSaveListener,NumberSaveAsListener,NumberLoadListener,NumberExitListener,NumberWelcomeListener,NumberQuestionListener,NumberSignalListener,NumberElaborationListener{

	private static final long serialVersionUID = 4209407506979960534L;
	
	/**indica la posizione nella avaiableviews della welcome view*/
	private static final int INDEX_WELCOME=0;
	/**indica la posizione nella avaiableviews della question view*/
	private static final int INDEX_QUESTION=1;
	/**indica la posizione nella avaiableviews della elaboration view*/
	private static final int INDEX_ELABORATION=3;
	/**indica la posizione nella avaiableviews della signal view*/
	private static final int INDEX_SIGNAL=2;
	
	/**menu dell'applicazione*/
	private static SignalSupporterMainMenu menu;
	/**lista delle view disponibili*/
	private static List<ConcettualView> avaiableviews;
	/**View mostrata al momento*/
	private static ConcettualView currentview;
	/**indica la scelta fatta dalla l'utente nel menu*/
	private static ListenedNumber<Integer> menuchoice;
	
	/**costurisce una nuova finestra del SignalTheory Supporter. Questa finestra visualizza una particolare View ma contiene anche tutte le
	 * altre possibili view. La finestra viene inizializzata con una view particolare (la WelcomeView). in seguito si possono scegliere altre view
	 * affinche' il programma funzioni correttamente, il costruttore deve elencare in questo preciso ordine le view:
	 * <table border="3">
	 *  <tr>
	 *   <td>1</td>
	 *   <td>Welcome View</td>
	 *  </tr>
	 *  <tr>
	 *   <td>2</td>
	 *   <td>Question View</td>
	 *  </tr>
	 *  <tr>
	 *   <td>3</td>
	 *   <td>Signal View</td>
	 *  </tr>
	 *  <tr>
	 *   <td>4</td>
	 *   <td>Elaboration View</td>
	 *  </tr>
	 * </table>
	 * 
	 * @param views
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public SignalSupporterMainFrame(ConcettualView ...views) throws FileNotFoundException, IOException{
		super();
		//this.setLayout(new GridLayout());
		avaiableviews=Arrays.asList(views);
		menu=new SignalSupporterMainMenu(this);
		menuchoice=new ListenedNumber<Integer>(0);
		menuchoice.addNumberListener(this);
		this.setJMenuBar(menu);
		swapViews(INDEX_WELCOME);
		assert !(currentview==null) : "current view nulla";
		this.setVisible(true);
	}
	private void swapViews(int newview){
		if (currentview!=null){
			this.remove(currentview.getView());
		}
		currentview=avaiableviews.get(newview);
		this.add(currentview.getView());
		currentview.getView().setVisible(true);
		this.repaint();
		this.validate();
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		menuchoice.setValue(((JIDMenuItem)e.getSource()).getId());
	}

	@Override
	public void SignalPressed(NumberEvent<Integer> arg0) {
		this.swapViews(INDEX_SIGNAL);
	}
	
	@Override
	public void ElaborationPressed(NumberEvent<Integer> arg0) {
		this.swapViews(INDEX_ELABORATION);
	}

	@Override
	public void QuestionPressed(NumberEvent<Integer> arg0) {
		this.swapViews(INDEX_QUESTION);
	}

	@Override
	public void WelcomePressed(NumberEvent<Integer> arg0) {
		this.swapViews(INDEX_WELCOME);
	}

	@Override
	public void ExitPressed(NumberEvent<Integer> arg0) {
		this.dispose();
	}

	@Override
	public void LoadPressed(NumberEvent<Integer> arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void SaveAsPressed(NumberEvent<Integer> arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void SavePressed(NumberEvent<Integer> arg0) {
		// TODO Auto-generated method stub
		
	}
	
}
